package com.gxar.quick.ar.server.db.service;

import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.quick.ar.server.base.common.enums.*;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import com.gxar.quick.ar.server.base.model.user.vo.response.UserRightsResponse;
import com.gxar.quick.ar.server.base.model.works.mapper.WorksMapStruct;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksInfoResponse;
import com.gxar.quick.ar.server.db.mapper.UserWorksMapper;
import com.gxar.quick.ar.server.db.model.UserWorksEntity;
import com.gxar.quick.ar.server.db.model.WorksEntity;
import com.gxar.quick.ar.server.db.repository.WorksRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/24 15:08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FavoritesService {

    private final WorksRepository worksRepository;
    private final UserWorksMapper userWorksMapper;

    private final UserService userService;

    /**
     * 获取作品收藏
     *
     * @param userProfile 用户信息
     * @param id          作品id
     * @return 作品是否收藏
     */
    @Transactional
    public Result<WorksInfoResponse> getWorksFavorites(UserProfile userProfile, Long id) {
        Optional<WorksEntity> worksEntityOptional = worksRepository.findById(id);
        if (worksEntityOptional.isPresent()) {
            WorksEntity worksEntity = worksEntityOptional.get();
            // 作品已下线
            if (WorksStatusEnum.OFFLINE.getCode().equals(worksEntity.getStatus())) {
                return Result.error(ResultCodeEnum.WORK_SHARE_INFO_OFFLINE);
            }
            // 作品已删除
            if (DeletedEnum.TRUE.getCode().equals(worksEntity.getDeleted())) {
                return Result.error(ResultCodeEnum.WORK_SHARE_INFO_INVALID);
            }
            // 作品识别次数已上限
            UserProfile worksUser = new UserProfile();
            worksUser.setId(worksEntity.getUserId());
            UserRightsResponse rightsResponse = userService.userRights(worksUser, ComboConfigItemEnum.IMPRESSIONS.getItem());
            if (Objects.isNull(rightsResponse) || (rightsResponse.getAvailableImpressionsNumber() <= 0 && rightsResponse.getTotalImpressionsNumber() > 0)) {
                return Result.error(ResultCodeEnum.WORKS_IMPRESSIONS_LIMIT);
            }
            WorksInfoResponse worksInfoResponse = WorksMapStruct.INSTANCE.entityToInfoResponse(worksEntity);
            List<Integer> userWorkSource = Arrays.asList(UserWorksSourceEnum.PLAZA.getCode(), UserWorksSourceEnum.SELF.getCode(), UserWorksSourceEnum.SHARE.getCode());
            List<UserWorksEntity> userWorksEntity = userWorksMapper.queryByWorksAndSources(userProfile.getId(), id, UserWorksTypeEnum.FAVORITES.getCode(), userWorkSource);
            if (CollectionUtils.isNotEmpty(userWorksEntity)) {
                worksInfoResponse.setFavorites(1);
            } else {
                worksInfoResponse.setFavorites(0);
            }
            worksInfoResponse.setWatermark(rightsResponse.getWatermark());
            return Result.success(worksInfoResponse);

        }
        return Result.error(ResultCodeEnum.WORK_SHARE_INFO_INVALID);
    }

}
